<template>
	<view class="one">
		<view class="nav">
			<!-- 顶部操作栏 -->
			<view class="top-bar">
				<text class="cancel" @click="goBack">取消</text>
				<text class="confirm" @click="confirmSelect">确认</text>
			</view>
			
			<!-- 头像选择区域 -->
			<view class="avatar-section">
				<view class="avatar-grid">
					<view class="avatar-item" 
						v-for="(item, index) in avatarList" 
						:key="index"
						@click="selectAvatar(index)"
					>
						<image class="avatar-img" :src="item" mode="aspectFill"></image>
						<view class="select-circle" :class="{'selected': selectedIndex === index}"></view>
					</view>
					<!-- 上传按钮 -->
					<view class="avatar-item upload-btn" @click="chooseImage">
						<text class="upload-text">+</text>
						<text class="desc-text">上传照片</text>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				selectedIndex: -1,
				avatarList: [
					'/static/tx.jpg',
					'/static/tx.jpg',
					'/static/tx.jpg',
					'/static/tx.jpg',
					'/static/tx.jpg'
				]
			}
		},
		methods: {
			// 选择预设头像
			selectAvatar(index) {
				this.selectedIndex = index;
			},
			
			// 选择本地图片
			chooseImage() {
				uni.chooseImage({
					count: 1, // 只选一张图片
					sizeType: ['compressed'], // 压缩图片
					sourceType: ['album', 'camera'], // 可以从相册选择或拍照
					success: (res) => {
						// 将选择的图片添加到头像列表
						const tempFilePath = res.tempFilePaths[0];
						this.avatarList.push(tempFilePath);
						// 自动选中新上传的图片
						this.selectedIndex = this.avatarList.length - 1;
					},
					fail: (err) => {
						uni.showToast({
							title: '选择图片失败',
							icon: 'none'
						});
					}
				});
			},
			
			// 确认选择
			confirmSelect() {
				if(this.selectedIndex === -1) {
					uni.showToast({
						title: '请选择头像',
						icon: 'none'
					});
					return;
				}
				
				const selectedAvatar = this.avatarList[this.selectedIndex];
				
				// 更新本地存储
				uni.setStorageSync('userAvatar', selectedAvatar);
				
				// 发送更新事件
				uni.$emit('updateAvatar', selectedAvatar);
				
				// 返回上一页
				uni.navigateBack({
					delta: 1,
					success: () => {
						uni.showToast({
							title: '头像已更新',
							icon: 'success'
						});
					}
				});
			},
			
			// 返回上一页
			goBack() {
				uni.redirectTo({
					url: '/packagesUpkeep/MyInformation/MyInformation'
				});
			}
		}
	}
</script>

<style>
page{
	background:#F7F7F7;
}  

.one{
	padding-top: 20rpx;
	height: 300rpx;
	background-color: #2D80F9;
	width: 100%;
	position: relative;  /* 改为相对定位 */
}

.nav{
	width: 680rpx;
	background-color: white;
	min-height: 200rpx;
	height: auto;
	border-top-left-radius: 30rpx;
	border-top-right-radius: 30rpx;
	border-bottom-left-radius: 30rpx;
	border-bottom-right-radius: 30rpx;
	margin: 0 auto;
	padding-bottom: 40rpx;
	position: relative;
	z-index: 2;
}

/* 顶部操作栏样式 */
.top-bar {
	display: flex;
	justify-content: space-between;
	padding: 30rpx 40rpx;
	border-bottom: 2rpx solid #F6F6F6;
}

.cancel {
	color: #333;
	font-size: 32rpx;
}

.confirm {
	color: #2D80F9;
	font-size: 32rpx;
}

/* 头像选择区域样式 */
.avatar-section {
	padding: 40rpx 30rpx;
	width: 100%;
	box-sizing: border-box;
}

.avatar-grid {
	display: flex;
	flex-wrap: wrap;
	gap: 30rpx;
	width: 100%;
	box-sizing: border-box;
	justify-content: flex-start;  /* 改为靠左对齐 */
	padding: 0 20rpx;  /* 添加左右内边距来调整整体位置 */
}

.avatar-item {
	position: relative;
	width: 170rpx;
	height: 170rpx;
	border-radius: 20rpx;
	overflow: hidden;
}

.avatar-img {
	width: 100%;
	height: 100%;
}

.select-circle {
	position: absolute;
	left: 10rpx;
	top: 10rpx;
	width: 24rpx;
	height: 24rpx;
	border-radius: 50%;
	background-color: #FFFFFF;  /* 改为白色背景 */
	border: 2rpx solid #CCCCCC;  /* 默认灰色边框 */
	transition: all 0.3s ease;  /* 添加过渡效果 */
}

.select-circle.selected {
	border-color: #2D80F9;  /* 选中时边框变为浅蓝色 */
	box-shadow: 0 0 10rpx rgba(45, 128, 249, 0.3);  /* 添加发光效果 */
}

.select-circle.selected::after {
	content: '';
	position: absolute;
	left: 50%;
	top: 50%;
	transform: translate(-50%, -50%);
	width: 12rpx;
	height: 12rpx;
	background-color: #2D80F9;  /* 实心圆改为浅蓝色 */
	border-radius: 50%;
	box-shadow: 0 0 8rpx #2D80F9;  /* 内圆发光效果 */
	animation: pulse 1.5s infinite;  /* 添加脉冲动画 */
}

/* 添加脉冲动画 */
@keyframes pulse {
	0% {
		box-shadow: 0 0 8rpx #2D80F9;
	}
	50% {
		box-shadow: 0 0 15rpx #2D80F9;
	}
	100% {
		box-shadow: 0 0 8rpx #2D80F9;
	}
}

.upload-btn {
	display: flex;
	flex-direction: column;
	justify-content: center;
	align-items: center;
	background-color: #F7F7F7;
	border: none;
}

.upload-text {
	font-size: 60rpx;
	color: #999;
	font-weight: 200;
	line-height: 1;
	margin-bottom: 10rpx;
}

.desc-text {
	font-size: 24rpx;
	color: #999;
}
</style>
